package org.gdstash.ui;

import org.gdstash.ui.util.AdjustableTabbedPane;
import org.gdstash.util.GDImagePool;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

public class GDTabbedPane extends AdjustableTabbedPane {
    private GDStashFrame frame;
    private String[] titles;
    private Icon[] icons;

    public GDTabbedPane(final GDStashFrame frame) {
        this.titles = new String[8];
        this.icons = new Icon[8];
        this.frame = frame;
        this.add(frame.pnlTransfer);
        this.add(frame.pnlCharInventory);
        if (GDStashFrame.iniConfig.sectRestrict.allowEdit) {
            this.add(frame.pnlCraft);
        }
        if (GDStashFrame.iniConfig.sectRestrict.allowEdit) {
            this.add(frame.pnlCharEdit);
        }
        this.add(frame.pnlMasteryInfo);
        this.add(frame.pnlCollection);
        this.add(frame.pnlMassImport);
        this.add(frame.pnlConfig);
        this.adjustUI();
        this.addChangeListener(new TabChangeListener());
        this.checkTabs();
        if (!GDStashFrame.dbConfig.configInit || !GDStashFrame.dbConfig.gddbInit) {
            this.setSelectedIndex(this.getTabCount() - 1);
        }
    }

    @Override
    public void adjustUI() {
        final Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntTabbed = UIManager.getDefaults().getFont("TabbedPane.font");
        fntTabbed = fntTabbed.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        this.setFont(fntTabbed);
        int index = 0;
        this.titles[0] = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_ITEM_TRANS");
        this.titles[1] = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_CHAR_TRANS");
        this.titles[2] = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_CRAFTING");
        this.titles[3] = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_CHAR_EDIT");
        this.titles[4] = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_MASTERY_INFO");
        this.titles[5] = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_COLLECTION");
        this.titles[6] = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_MASS_IMPORT");
        this.titles[7] = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_CONFIG");
        this.icons[0] = GDImagePool.iconTabStashTransfer16;
        this.icons[1] = GDImagePool.iconTabCharTransfer16;
        this.icons[2] = GDImagePool.iconTabStashCraft16;
        this.icons[3] = GDImagePool.iconTabCharEdit16;
        this.icons[4] = GDImagePool.iconTabMasteryInfo16;
        this.icons[5] = GDImagePool.iconTabCollection16;
        this.icons[6] = GDImagePool.iconTabFileImp16;
        this.icons[7] = GDImagePool.iconTabConfigure16;
        this.setTitleAt(index, this.titles[0]);
        this.setIconAt(index, this.icons[0]);
        ++index;
        this.setTitleAt(index, this.titles[1]);
        this.setIconAt(index, this.icons[1]);
        ++index;
        if (GDStashFrame.iniConfig.sectRestrict.allowEdit) {
            this.setTitleAt(index, this.titles[2]);
            this.setIconAt(index, this.icons[2]);
            ++index;
        }
        if (GDStashFrame.iniConfig.sectRestrict.allowEdit) {
            this.setTitleAt(index, this.titles[3]);
            this.setIconAt(index, this.icons[3]);
            ++index;
        }
        this.setTitleAt(index, this.titles[4]);
        this.setIconAt(index, this.icons[4]);
        ++index;
        this.setTitleAt(index, this.titles[5]);
        this.setIconAt(index, this.icons[5]);
        ++index;
        this.setTitleAt(index, this.titles[6]);
        this.setIconAt(index, this.icons[6]);
        ++index;
        this.setTitleAt(index, this.titles[7]);
        this.setIconAt(index, this.icons[7]);
        ++index;
    }

    public void checkTabs() {
        final boolean enable = GDStashFrame.dbConfig.configInit && GDStashFrame.dbConfig.gddbInit;
        for (int count = this.getTabCount(), i = 0; i < count - 1; ++i) {
            this.setEnabledAt(i, enable);
        }
    }

    public void setEdit(final boolean allowed) {
        final int tabs = this.getTabCount();
        if (allowed) {
            if (tabs == 7) {
                return;
            }
            this.insertTab(this.titles[2], this.icons[2], this.frame.pnlCraft, null, 2);
            this.insertTab(this.titles[3], this.icons[3], this.frame.pnlCharEdit, null, 3);
        } else {
            if (tabs == 5) {
                return;
            }
            this.removeTabAt(3);
            this.removeTabAt(2);
        }
    }

    private class TabChangeListener implements ChangeListener {
        @Override
        public void stateChanged(final ChangeEvent e) {
            final Component comp = GDTabbedPane.this.getSelectedComponent();
            if (comp == GDTabbedPane.this.frame.pnlTransfer) {
                GDTabbedPane.this.frame.pnlTransfer.checkSaveButton();
                GDTabbedPane.this.frame.pnlTransfer.updateStash();
            }
            if (comp == GDTabbedPane.this.frame.pnlCraft) {
                GDTabbedPane.this.frame.pnlCraft.checkSaveButton();
                GDTabbedPane.this.frame.pnlCraft.updateStash();
            }
        }
    }
}
